#include "UserInfo.h"
#include <QFile>
#include <QDataStream>
#include <iostream>
#include <QCoreApplication>

UserInfo::UserInfo()
{
    readUserData();
}

bool UserInfo::Register(QString user, QString pwd)
{
    bool ret = true;

    int temp = user.toInt(&ret);
    if(ret && (temp/100==0))
    {
        return false;
    }
    ret = true;

    for(int i=0; i<mUserList.count(); i++)
    {
        if(mUserList[i].username == user)
        {
            ret = false;
            break;
        }
    }

    if(ret)
    {
        User u;
        u.username = user;
        u.password = pwd;
        u.roomID = -1;
        u.status = DISCONNECTED;
        u.lastStatus = DISCONNECTED;
        u.socket = NULL;
        u.isAbnormalExit = false;

        mUserList.append(u);
        addUserData(user, pwd);
    }

    return ret;
}

bool UserInfo::Login(QString user, QString pwd, QTcpSocket* socket)
{
    bool ret = false;

    for(int i=0; i<mUserList.count(); i++)
    {
        if(mUserList[i].username == user)
        {
            if(mUserList[i].password == pwd)
            {
                ret = true;
                mUserList[i].status = LOGIN;
                mUserList[i].socket = socket;
                break;
            }
        }
    }

    return ret;
}

int UserInfo::getRoom(QString user)
{
    int ret = -1;
    int i = indexOf(user);

    if(i != -1)
    {
        ret = mUserList[i].roomID;
    }

    return ret;
}

void UserInfo::setRoom(QString user, int room)
{
    int i = indexOf(user);

    if(i != -1)
    {
        mUserList[i].roomID = room;
    }
}

UserInfo::Status UserInfo::getStatus(QString user)
{
    Status ret = DISCONNECTED;
    int i = indexOf(user);

    if(i != -1)
    {
        ret = mUserList[i].status;
    }

    return ret;
}

UserInfo::Status UserInfo::getLastStatus(QString user)
{
    Status ret = DISCONNECTED;
    int i = indexOf(user);

    if(i != -1)
    {
        ret = mUserList[i].lastStatus;
    }

    return ret;
}

void UserInfo::setStatus(QString user, Status s)
{
    int i = indexOf(user);

    if(i != -1)
    {
        mUserList[i].lastStatus = mUserList[i].status;
        mUserList[i].status = s;
    }
}

void UserInfo::setStatus(int index, Status s)
{
    if(index>=mUserList.count() || index<0)
        return;
    mUserList[index].lastStatus = mUserList[index].status;
    mUserList[index].status = s;
}

void UserInfo::setStatus(QTcpSocket* socket, Status s)
{
    for(int i=0; i<mUserList.count(); i++)
    {
        if(mUserList[i].socket == socket)
        {
            mUserList[i].lastStatus = mUserList[i].status;
            mUserList[i].status = s;
            break;
        }
    }
}

int UserInfo::indexOf(QString user)
{
    int ret = -1;

    for(int i=0; i<mUserList.count(); i++)
    {
        if(mUserList[i].username == user)
        {
            ret = i;
            break;
        }
    }

    return ret;
}

void UserInfo::setIsAbnormalExit(QString user, bool b)
{
    int i = indexOf(user);

    if(i != -1)
    {
        mUserList[i].isAbnormalExit = b;
    }
}

bool UserInfo::isAbnormalExit(QString user)
{
    int i = indexOf(user);

    if(i != -1)
    {
        return mUserList[i].isAbnormalExit;
    }
    else
    {
        return false;
    }
}

void UserInfo::setSocket(QString user, QTcpSocket* socket)
{
    int i = indexOf(user);

    if(i != -1)
    {
        mUserList[i].socket = socket;
    }
}

void UserInfo::readUserData()
{
    QFile file(QCoreApplication::applicationDirPath() + "/userinfo.dat");

    if(file.open(QIODevice::ReadOnly))
    {
        QDataStream in(&file);

        while(!in.atEnd())
        {
            User u;
            in >> u.username;
            in >> u.password;
            u.roomID = -1;
            u.status = DISCONNECTED;
            u.lastStatus = DISCONNECTED;
            u.socket = NULL;
            u.isAbnormalExit = false;
            mUserList.append(u);
        }

        file.close();
    }
    else
    {
        std::cout << "open file error: userinfo.dat" << std::endl;
    }
}

void UserInfo::addUserData(QString user, QString pwd)
{
    QFile file(QCoreApplication::applicationDirPath() + "/userinfo.dat");

    if(file.open(QIODevice::Append))
    {
        QDataStream out(&file);

        for(int i=0; i<mUserList.count(); i++)
        {
            out << user;
            out << pwd;
        }

        file.close();
    }
    else
    {
        std::cout << "open file error: userinfo.dat" << std::endl;
    }
}

QString UserInfo::getUsername(QTcpSocket* socket)
{
    QString user = "";

    for(int i=0; i<mUserList.count(); i++)
    {
        if(mUserList[i].socket == socket)
        {
            user = mUserList[i].username;
            break;
        }
    }

    return user;
}
